var httpsUrl = getApp().globalData.httpsUrl;
var globalData = getApp().globalData;
var commonUtil = require("../../../utils/commonUtil.js")
var imgUrl = globalData.imgUrl;
var checkNetWork = require("../../../utils/CheckNetWork.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    imgUrl: imgUrl,
    //车牌相关变量
    isKeyboard: false,//是否显示键盘
    specialBtn: false,//false显示第一页，true显示第二页
    tapNum: false,//数字键盘是否可以点击
    isFocus: false,//输入框聚焦
    flag: false,//防止多次点击的阀门
    keyboardNumber: '1234567890',//数字集合
    keyboardAlph: 'QWERTYUIOPASDFGHJKL巛ZXCVBNM',//字母集合，删除按钮<<<
    keyboard1: '京津沪冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤川青藏琼宁渝',//首页键盘,显示省的简称
    keyboard2: '',//第二页键盘，显示数字和大写字母
    keyboardValue: '',
    textArr: [],
    textValue: '',
    chassisvin:'',//车架号
    engineno: '',//发动机号
  },
  
  //查询
  peccancySelectResult: function () {
    var data = this.data;
    if (commonUtil.isEmpty(data.textValue) || data.textArr.length < 7) {
      //commonUtil.showErrorToast("车牌号错误");
      //return;
    }
    
    var list = new Object();
    let vehicleno = data.textValue;
    list.lsprefix = vehicleno.substring(0, 1);
    list.lsnum = vehicleno.substring(1, vehicleno.length);
    list.frameno = data.chassisvin;
    list.engineno = data.engineno;

    // list.lsprefix = "沪";
    // list.lsnum = "E42717";
    // list.frameno = "181586";
    // list.engineno = "985969";
    // list.carorg = "shanghai";

    console.info("参数:"+JSON.stringify(list));
    // let openId = wx.getStorageSync("openidKey");
    commonUtil.showLoading("稍等...");
    wx.request({
      //header: { "openId": openId, "content-Type": "application/x-www-form-urlencoded" },
      url: httpsUrl + "/Illegal/getCarIllegalData",
      dataType: "json",
      method: "POST",
      data: JSON.stringify(list),
      success: function (res) {
        commonUtil.hideLoading();
        let result = res.data;
        console.info("违章查询返回：" + JSON.stringify(result));
        if (result.code == 0) {
          let rows = result.data.rows;
          wx.setStorageSync(globalData.peccancySelectResultKey,rows);
          wx.setStorageSync(globalData.carNoKey, list.vehicleno);
          wx.navigateTo({
            url: '../peccancySelectResult/peccancySelectResult',
          })
        } else {
          commonUtil.showModal("失败：" + result.message);
        }
      }
    })
  },
  chassisvinChange: function (e) {
    this.setData({
      chassisvin: e.detail.value
    })
  },
  enginenoChange: function (e) {
    this.setData({
      engineno: e.detail.value
    })
  },

  //输入框聚焦触发
  bindFocus: function () {
    this.setData({
      isKeyboard: !this.data.isKeyboard,
      isFocus: true,
    })
  },
  /**
   * 键盘事件
   */
  tapKeyboard: function (e) {
    var self = this;
    //获取键盘点击的内容，并将内容赋值到框中
    var tapIndex = e.target.dataset.index;
    var tapVal = e.target.dataset.val;
    var keyboardValue;
    var specialBtn;
    var tapNum;
    if (tapVal == "巛") {
      //说明是删除
      self.data.textArr.pop();
      if (self.data.textArr.length == 0) {
        //说明没有数据了，返回到省份选择键盘
        this.specialBtn = false;
        this.tapNum = false;
        this.keyboardValue = self.data.keyboard1;
      } else if (self.data.textArr.length == 1) {
        //只能输入字母
        this.tapNum = false;
        this.specialBtn = true;
        this.keyboardValue = self.data.keyboard2;
      } else {
        this.specialBtn = true;
        this.tapNum = true;
        this.keyboardValue = self.data.keyboard2;
      }
      self.data.textValue = self.data.textArr.join("");
      self.setData({
        textValue: self.data.textValue,
        keyboardValue: this.keyboardValue,
        specialBtn: this.specialBtn,
        tapNum: this.tapNum,
      })
      return false
    }
    if (self.data.textArr.length >= 7) {
      return false;
    }
    self.data.textArr.push(tapVal);
    self.data.textValue = self.data.textArr.join("");
    self.setData({
      textValue: self.data.textValue,
      keyboardValue: self.data.keyboard2,
      specialBtn: true,
    })
    if (self.data.textArr.length > 1) {
      self.setData({
        tapNum: true //展示数字键盘
      })
    }
  },
  //键盘完成按钮事件
  tapSpecBtn: function () {
    var self = this;
    // if (self.data.flag) {
    //   return false
    // }
    if (self.data.textValue.length < 7) {
      commonUtil.showErrorToast("车牌号错误");
    } else {
      this.hideKeyboard();
      self.setData({
        flag: true
      })
    }
  },
  //车牌‘省’列表通过接口更新
  onLoadCar: function (options) {
    var that = this;
    if (!checkNetWork.checkNetWorkStatu()) {
      console.log('网络错误')
      return false;
    } else {
      wx.request({
        url: 'https://parkinglot.qqdayu.com/parking/home',
        data: { textValue: that.data.textValue },
        header: {'content-type': 'application/x-www-form-urlencoded'},
        success: function (res) {
          var response = res.data.data;
          console.info("response:" + JSON.stringify(response));
          that.setData({
            keyboard1: response.keyboard1
          })
        }
      })
    }
  },
  //显示键盘
  showKeyboard: function () {
    this.setData({
      isFocus: true,
      isKeyboard: true,
    })
  },
  //隐藏键盘
  hideKeyboard: function () {
    if (this.data.isKeyboard) {
      this.setData({
        isKeyboard: false,
        isFocus: false
      })
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.removeStorageSync(globalData.carNoKey);
    wx.removeStorageSync(globalData.peccancySelectResultKey);
    // wx.request({
    //   header: {"content-Type": "application/x-www-form-urlencoded" },
    //   url: httpsUrl + "/Illegal/getCondition",
    //   dataType: "json",
    //   method: "POST",
    //   data: { "lsprefix": "沪","lsnum":"E"},
    //   success: function (res) {
    //     let result = res.data;
    //     console.info("res：" + JSON.stringify(res));
    //   }
    // })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    //将keyboard1和keyboard2中的所有字符串拆分成一个一个字组成的数组
    this.data.keyboard1 = this.data.keyboard1.split('')
    this.data.keyboard2 = this.data.keyboard2.split('')
    this.setData({
      keyboardValue: this.data.keyboard1
    })
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      flag: false
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})